<template>
  <div>
    <!-- close:所有的、任意形式的关闭弹窗 -->
    <el-dialog
      :title="info.isAdd ? '添加管理员' : '修改管理员'"
      :visible.sync="info.isShow"
      width="50%"
      @close="cancel"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="specsname" label="属性名">
          <el-input v-model="ruleForm.specsname"></el-input>
        </el-form-item>
        <el-form-item label="属性值" prop="attrs">
          <div v-for="(item, index) in attrsList" :key="index">
            <el-input v-model="attrsList[index]"></el-input>
            <el-button v-if="index === 0" @click="addAttrs">新增域名</el-button>
            <el-button v-else @click.prevent="removeAttrs(index)"
              >删除</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入负责添加角色的函数
import {} from "../../request";
// 弹窗的默认值
let defaultData = {
  specsname: "",
  attrs: "",
  status: 1,
};
export default {
  props: {
    info: {
      type: Object,
      default: {
        isAdd: false,
        isShow: false,
      },
    },
  },
  data() {
    return {
      // 属性值的数组
      attrsList: [""],
      // 表单的数据
      ruleForm: { specsname: '', attrs: "", status: 1 },
      // 表单的规则
      rules: {
        roleid: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 设置当前组件状态
    setStatus(val) {
      this.ruleForm = { ...val };
      // 给属性值的数组赋值
      this.attrsList = [...this.ruleForm.attrs];
      // attrs后台需要字符串格式
      this.ruleForm.attrs = this.attrsList.join();
    },
    // 提交表单
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let arr = [];
          this.attrsList.forEach(item => {
            arr.push(item)
          })
          this.ruleForm.attrs = arr.join();
          console.log(this.ruleForm);
          return
          // 封装请求
          let res;
          if (this.info.isAdd) {
            res = userAdd(this.ruleForm);
          } else {
            res = userEdit(this.ruleForm);
          }
          if (res) {
            this.cancel();
            // 兄弟组件传值，告诉list组件更新数据
            this.$bus.$emit("update");
          }
        } else {
          return false;
        }
      });
    },
    // 增加一项
    addAttrs() {
      this.attrsList.push("");
    },
    // 删除一项
    removeAttrs(index) {
      this.attrsList.splice(index, 1)
    },
    // 点击取消，清空数据
    cancel() {
      // 重置弹窗中的数据
      this.ruleForm = { ...defaultData };
      // 重置attrsList，方便重新输入属性值
      this.attrsList = [""];
      // 关闭弹窗
      this.info.isShow = false;
    },
  },
};
</script>

<style>
</style>